package com.atom.app;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import android.content.Context;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.atom.annotation.ApiImpl;
import com.atom.annotation.api.ApiCLassFilter;
import com.atom.annotation.api.ApiImplCallback;
import com.atom.annotation.api.ApiImplContext;
import com.atom.annotation.bean.ApiImplWrapper;
import com.atom.base.app.Hello;
import com.atom.core.ApiImplRouter;
import com.atom.module.logger.Logger;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collection;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class Test_ApiImpl_GetApis {

    @Before
    public void init() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Logger.init(true, false, "");
    }

    // 获取指定的接口或者超类 所被继承或者实现的子类集合
    @Test
    public void testApis() {
        ApiImplRouter instance = ApiImplRouter.getInstance();
        instance.getApis(Hello.class, new ApiImplCallback<Collection<Class<? extends Hello>>>() {
            @Override
            public void call(ApiImplContext apiImplContext, Collection<Class<? extends Hello>> classes) {
                Logger.e(classes.size(), classes);
            }
        });
    }

    // 获取指定的接口或者超类 根据精确的名称获取
    @Test
    public void testApis_2() {
        ApiImplRouter instance = ApiImplRouter.getInstance();

        instance.getApis(Hello.class, "hello2", false, new ApiImplCallback<Collection<Class<? extends Hello>>>() {
            @Override
            public void call(ApiImplContext apiImplContext, Collection<Class<? extends Hello>> classes) {
                Logger.e(classes.size(), classes);
                assertEquals(classes.size(), 5);
            }
        });
    }

    // 获取指定的接口或者超类 根据正则获取
    @Test
    public void testApis_3() {
        ApiImplRouter instance = ApiImplRouter.getInstance();
        instance.getApis(Hello.class, "[A-Za-z0-9]+", true, new ApiImplCallback<Collection<Class<? extends Hello>>>() {
            @Override
            public void call(ApiImplContext apiImplContext, Collection<Class<? extends Hello>> classes) {
                Logger.e(classes.size(), classes);
            }
        });
    }

    // 获取指定的接口或者超类 根据过滤器进行选择
    @Test
    public void testApis_4() {
        ApiImplRouter instance = ApiImplRouter.getInstance();
        instance.getApis(Hello.class, new ApiCLassFilter<Hello>() {
            @Override
            public boolean accept(ApiImplContext apiImplContext, Class<? extends Hello> clazz, ApiImplWrapper<Hello> param) {
                if (param.version > 2) return true;
                return false;
            }
        }, new ApiImplCallback<Collection<Class<? extends Hello>>>() {
            @Override
            public void call(ApiImplContext apiImplContext, Collection<Class<? extends Hello>> classes) {
                StringBuilder builder = new StringBuilder() ;
                for (Class<? extends Hello> aClass : classes) {
                    builder.append(get(aClass)).append("\n");
                }
                Logger.e(classes.size(), builder.toString());
            }
        });
    }

    public ApiImplWrapper<?> get(Class<?> clazz) {
        ApiImpl annotation = clazz.getAnnotation(ApiImpl.class);
        return new ApiImplWrapper(annotation.api(), annotation.provider(), annotation.name(), annotation.version());
    }
}